package servlet;

import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import dao.Dao;
import domain.Goods;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Calendar;

@WebServlet("/updateGoodServlet")
public class UpdateGoodServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String ext = "", url = "upload/";
        SmartUpload su = new SmartUpload();  //创建新的上传对象
        long maxsize = 20000 * 1024; //设置每个上传文件最大为20000KB
        String info;
        try {
            su.initialize(this.getServletConfig(), request, response); //初始化
            su.setMaxFileSize(maxsize); //限制上传文件的大小
            su.setAllowedFilesList("jpg,JPG,gif,GIF,png,PNG"); //设定允许上传的格式
            su.upload(); //上传文件
            Request req = su.getRequest(); //创建Request对象
            String cid = request.getParameter("cid");
            String cname = req.getParameter("cname");
            String cname_no1 = req.getParameter("cname_no1");
            String cname_no2 = req.getParameter("cname_no2");
            String number = req.getParameter("cnumber");
            String price = req.getParameter("cprice");
            String cintro = req.getParameter("cintro");
            String discounte = req.getParameter("cdiscounte");
            Goods goods = new Goods();
            Dao dao = new Dao();
            int cnumber = Integer.parseInt(number);
            float cprice = Float.parseFloat(price);
            float cdiscounte = Float.parseFloat(discounte);
            //获取上传的文件，因为只上传了一个文件，所以可直接获取
            com.jspsmart.upload.File myfile = su.getFiles().getFile(0);
            if (!myfile.isMissing()) {
                ext = myfile.getFileExt();  //取得扩展名
                Calendar cal = Calendar.getInstance();
                String filename = String.valueOf(cal.getTimeInMillis());
                String saveurl = request.getRealPath("/") + url;
                System.out.println(saveurl);
                saveurl += filename + "." + ext;  //saveurl为保存图片的绝对路径
                String cimage = url + filename + "." + ext;
                goods.setCid(cid);
                goods.setCname(cname);
                goods.setCname_no1(cname_no1);
                goods.setCname_no2(cname_no2);
                goods.setCnumber(cnumber);
                goods.setCprice(cprice);
                goods.setCintro(cintro);
                goods.setCimage(cimage);
                goods.setCdiscounte(cdiscounte);
                int cmark = dao.updateGoods(goods);
                if (cmark == 1) {
                    //将文件保存到指定目录中
                    myfile.saveAs(saveurl, SmartUpload.SAVE_PHYSICAL);
                    response.sendRedirect(request.getContextPath()+"/findGoodsServlet");
                } else {
                    info = "修改失败！";
                    request.setAttribute("update_information", info);
                    request.getRequestDispatcher("/updateGood.jsp").forward(request, response);
                }

            } else {
                info = "请选择要上传的文件！";
                request.setAttribute("update_information", info);
                request.getRequestDispatcher("/updateGood.jsp").forward(request, response);
            }


        } catch (Exception e) {
            e.printStackTrace();
            info = "修改异常！";
            request.setAttribute("update_information", info);
            request.getRequestDispatcher("/updateGood.jsp").forward(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
